/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using FlickrNet;
using System.Net;

namespace ArtNotes
{

	/// <summary>
	/// Shows a selected image from flickr along with
	/// the images description underneath.
	/// This also provides the otion to save the image.
	/// </summary>
	public class ANFlickrImage : UIViewController
	{
		
		private UIScrollView pageScrollView;
		//private UIImageView imageScrollViewImage;
		
		private UIButton flickrImageView;

		private UIImage scaledPolaroidImage;
		private UIImageView polaroidBackground;
		private UIImageView polaroidFrame;
		private UIImage polaroidImage;
		
		private UIImageView postitImage;
		
		private Photo photo;
		public ANFlickrImage (Photo photo)
		{
			this.photo = photo;
		}
		
		public override void ViewDidLoad ()
		{
			this.View.Frame = new RectangleF(0,0,320,370);
			this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			UIBarButtonItem btnSave = new UIBarButtonItem();
			btnSave.Title = "Save";
			btnSave.Clicked += delegate {
				Console.WriteLine("Save Pressed");
			};
			
			this.NavigationItem.SetRightBarButtonItem(btnSave,true);
			
			this.Title = photo.Title;
		}
		
		public override void ViewDidAppear (bool animated)
		{
			polaroidFrame = new UIImageView();
			
			UIImage tmp;
			//if(photo.DoesMediumExist)
			//	tmp = downloadImage(photo.MediumUrl,photo.PhotoId + ".jpg");
			//else
				tmp = downloadImage(photo.MediumUrl,photo.PhotoId + ".jpg");
			
			//imageScrollViewImage = new UIImageView(tmp);
			//imageScrollViewImage.Frame = new RectangleF (0,0,270,250);
			
			
			polaroidFrame.Image = UIImage.FromFile("Images/large_polaroid.png");
			polaroidFrame.Frame = new RectangleF(30,5,266,283);
			
			polaroidImage = tmp;
			
			ANImageUtils anImageUtils = new ANImageUtils();
			
			scaledPolaroidImage = anImageUtils.scaleImage(polaroidImage,200, 220);
			
			polaroidBackground = new UIImageView(new RectangleF(60,25,200,220));
			
			polaroidBackground.Image = UIImage.FromFile("Images/polaroidBackground.png");
			
			PointF pf = new PointF();
			
			pf.X = 60 + ((200 - scaledPolaroidImage.Size.Width) / 2);
			pf.Y = 25 + ((220 - scaledPolaroidImage.Size.Height) / 2);
			
			
			flickrImageView = ImageButtonType(
			                                 new RectangleF(pf,scaledPolaroidImage.Size),
			                                 scaledPolaroidImage,
			                                 scaledPolaroidImage
			                                 );
			flickrImageView.TouchDown += delegate {
				//UIImage fsi = downloadImage(photo.SmallUrl,photo.PhotoId + ".jpg");
				ANFullScreenFlickrImage anFullScreenFlickrImage = new ANFullScreenFlickrImage(photo);
				this.NavigationController.PresentModalViewController(anFullScreenFlickrImage,true);
			};
			
			pageScrollView = new UIScrollView();

			pageScrollView.ContentSize = new SizeF(320f,470f);
			pageScrollView.Frame = new RectangleF (0, 0, 320, 370);
			pageScrollView.ClipsToBounds = true;
			pageScrollView.ScrollEnabled = true;
			pageScrollView.AlwaysBounceVertical = true;
			//pageScrollView.Bounds = this.Frame;
			pageScrollView.ShowsVerticalScrollIndicator = true;
			pageScrollView.DirectionalLockEnabled = true;
			
			pageScrollView.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			this.View.AddSubview(pageScrollView);
			
			Flickr flickr = AN_APP_OPTIONS.AN_FLICKR;
			PhotoInfo photoInfo = flickr.PhotosGetInfo(photo.PhotoId);
			Console.WriteLine(photoInfo.Description);
			
			UITextView flickrDescription = new UITextView();
			
			flickrDescription.Frame = new RectangleF(30,300,260,10390);
			flickrDescription.BackgroundColor = UIColor.Clear;//FromPatternImage(UIImage.FromFile("Images/postit_group.png"));
			flickrDescription.Font = UIFont.FromName ("Marker Felt", 11);
			flickrDescription.TextAlignment = UITextAlignment.Center;
			flickrDescription.TextColor = UIColor.DarkGray;
			flickrDescription.Editable = false;
			
			flickrDescription.Text = photoInfo.Description;
			
			postitImage = new UIImageView(new RectangleF(20,280,280,130));
			postitImage.Image = UIImage.FromFile("Images/postit_group.png");
			
			pageScrollView.AddSubview(polaroidFrame);
			pageScrollView.AddSubview(polaroidBackground);
			pageScrollView.AddSubview(flickrImageView);
			pageScrollView.AddSubview(postitImage);
			
			pageScrollView.AddSubview(flickrDescription);
		}

		public UIImage downloadImage (string url, string filename)
		{
			String docdir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			WebClient wc = new WebClient ();
			wc.DownloadFile (url, docdir + "/" + filename);
			UIImage tmp = UIImage.FromFileUncached(docdir + "/" + filename);
			return tmp;
		}
		
		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		/*public class scrollViewDelegate : UIScrollViewDelegate
		{
			private ANFlickrImage anFlickrImage;
			
			public scrollViewDelegate(ANFlickrImage anFlickrImage)
			{
				this.anFlickrImage = anFlickrImage;
			}
			
			public override UIView ViewForZoomingInScrollView (UIScrollView scrollView)
			{
				return this.anFlickrImage.imageScrollViewImage;
			}

		}*/
	}
}
